package com.seo.controller.index;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.seo.pojo.ReturnBody;
import com.seo.service.user.sdk.UserService;

import cn.hutool.core.lang.PatternPool;
import cn.hutool.core.util.ReUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * 注册controller <br/>
 * 
 * @author shibeilei
 * @version 2017.11.30 12:20
 */
@Slf4j
@Controller
@RequestMapping(value = "/register")
public class RegisterController {

	@Autowired
	private UserService userService;

	@RequestMapping
	public String register(HttpServletRequest request, HttpSession session) {
		log.info("进入注册页...");
		return "register";

	}

	@RequestMapping("/to")
	@ResponseBody
	public ReturnBody to(@RequestAttribute("domainUserId") int domainUserId, String userName, String password,
			String againPassword, String phone, String smsCode, String sInviteCode) {
		log.info("注册...");

		ReturnBody rb = ReturnBody.init();
		rb = userService.register(domainUserId, userName, password, againPassword, phone, smsCode, sInviteCode);
		if (!rb.isSuccess()) {
			return rb;
		}

		return rb;

	}

	@RequestMapping("/sendSms")
	@ResponseBody
	public ReturnBody sendSms(String phone) {
		log.info("注册-获取短信码...");

		ReturnBody rb = ReturnBody.init();
		if (StringUtils.isEmpty(phone)) {
			return rb.error("error", "手机号不能为空");
		}

		if (!ReUtil.contains(PatternPool.MOBILE, phone)) {
			log.error("手机号格式错误");
			return rb.error("请输入正确手机号");
		}

		rb = userService.saveRegisterSmsCode(phone);
		if (!rb.isSuccess()) {
			return rb;
		}

		return rb;

	}
}
